package com.seveneight.baijiesteward.ui.user.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.PayBackRecordAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.PayBackRecordBean;
import com.seveneight.baijiesteward.ui.main.activity.OrderDetailActivity;
import com.seveneight.baijiesteward.ui.main.contract.GetOrderListContract;
import com.seveneight.baijiesteward.ui.main.presenter.GetOrderListPresenter;
import com.seveneight.baijiesteward.utils.RecyclerviewInitUtil;
import com.seveneight.baijiesteward.utils.WindowUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Created by user1 on 2018/3/29.
 */

public class RecordActivity extends BaseActivity implements View.OnClickListener, GetOrderListContract.GetorderListView {

    GetOrderListContract.GetOrderListPresenter getOrderListPresenter;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.recyclerview)
    LRecyclerView recyclerView;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;
    private PayBackRecordAdapter payBackRecordAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private int page = 1;
    private String type;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_list);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getString("type");
            if (type.equals("1")) {
                tv_title.setText("还款记录");
            } else if (type.equals("3")) {
                tv_title.setText("提现记录");
            } else if (type.equals("6")) {
                tv_title.setText("支付记录");
            } else {
                tv_title.setText("账单");
            }
        }
    }

    @Override
    protected void initData() {
        RecyclerviewInitUtil.initLineRecycler(activity, recyclerView);
        payBackRecordAdapter = new PayBackRecordAdapter(activity);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(payBackRecordAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        getOrderListPresenter = new GetOrderListPresenter(this);
        getData();
    }

    private void getData() {
        getOrderListPresenter.getOrderData(activity, jsonFormat.orderList(type, page));
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("recordBean", payBackRecordAdapter.getDataList().get(position));
                WindowUtil.getInstance().startActivity(activity, OrderDetailActivity.class, bundle);
            }
        });
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                payBackRecordAdapter.clear();
                page = 1;
                getData();
            }
        });

        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view == iv_back) {
            finish();
        }
    }

    @Override
    public void rebackOrderData(List<PayBackRecordBean> data) {
        recyclerView.refreshComplete(data.size());
        if (page == 1 && data.size() == 0) {
            iv_empty.setVisibility(View.VISIBLE);
        } else {
            iv_empty.setVisibility(View.GONE);
        }
        if (data.size() == 0) {
            recyclerView.setNoMore(true);
        }
        payBackRecordAdapter.addAll(data);
        lRecyclerViewAdapter.notifyDataSetChanged();
    }
}
